package com.qf.smsplatform.service.Impl;
/*2021/10/31*/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.cache.TClientBusinessCache;
import com.qf.smsplatform.events.TClientBusinessEvent;
import com.qf.smsplatform.mapper.TClientBusinessMapper;
import com.qf.smsplatform.pojo.TClientBusiness;
import com.qf.smsplatform.service.TClientBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @YuTao
 */
@Service
@Transactional
public class TClientBusinessServiceImpl implements TClientBusinessService {

    private TClientBusinessCache tClientBusinessCache;

    private TClientBusinessMapper tClientBusinessMapper;
    @Autowired
    public void settClientBusinessMapper(TClientBusinessMapper tClientBusinessMapper) {
        this.tClientBusinessMapper = tClientBusinessMapper;
    }
    @Autowired
    public void settClientBusinessCache(TClientBusinessCache tClientBusinessCache) {
        this.tClientBusinessCache = tClientBusinessCache;
    }

    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public PageInfo<TClientBusiness> selectPage(int pageNum,int pageSize) {

        PageHelper.startPage(pageNum,  pageSize);
        List<TClientBusiness> allData = tClientBusinessCache.getAllData();
        List<TClientBusiness> collect = allData.stream()
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        PageInfo<TClientBusiness> pageInfo =new PageInfo<>(collect);

        return pageInfo;
    }

    @Override
    public void updateBusiness(TClientBusiness tClientBusiness) {

        tClientBusinessMapper.updateBusiness(tClientBusiness);
        //发布一个事件，对缓存进行更新
        context.publishEvent(new TClientBusinessEvent());
    }

    @Override
    public void insertBusiness(TClientBusiness tClientBusiness) {
        tClientBusinessMapper.insertBusiness(tClientBusiness);

        //发布一个事件，对缓存进行更新
        context.publishEvent(new TClientBusinessEvent());
    }

    @Override
    public void deleteBusiness(int id) {

        tClientBusinessMapper.deleteBusiness(id);

        //发布一个事件，对缓存进行更新
        context.publishEvent(new TClientBusinessEvent());
    }

    @Override
    public TClientBusiness selcetById(Long id) {

        TClientBusiness byKey = tClientBusinessCache.getByKey(id);

        return byKey;
    }
}
